<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>在线考试系统-课程管理</title>
    <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
	<link href="/res/css/style.css" rel="stylesheet">
    <style type="text/css"></style>
</head>
<body>

<?php include(APPLICATION_PATH.'application/views/index/header.html');?>

<div id="container">

    <div class="container-left">
        <?php include(APPLICATION_PATH.'application/views/index/left.html');?>
    </div>

    <div class="container-right well">
		<div class="tabSet"><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addcourseModal">添加课程</button></div>
		<table class="table table-bordered">
		   <thead>
			  <tr>
				<th>课程名称</th>
                <th>添加时间</th>
                <th>修改时间</th>
				<th>操作</th>
			  </tr>
		   </thead>
		   <tbody>
               <?php foreach($list as $i) {?>
                  <tr>
                    <td><?php echo $i['name'];?></td>
                    <td><?php echo $i['date'];?></td>
                    <td><?php echo $i['update_date'];?></td>
                    <td>
                        <button type="button" class="btn btn-primary update_course" data-toggle="modal" data-target="#updatecourseModal" _id="<?php echo $i['_id'];?>" course_name="<?php echo $i['name'];?>">编辑</button>
                        <button type="button" class="btn btn-danger delete_course" data-toggle="modal" data-target="#deletecourseModal" _id="<?php echo $i['_id'];?>">删除</button>
                    </td>
                  </tr>
               <?php }?>
           </tbody>
		</table>
    </div>

</div>
<div class="modal fade" id="addcourseModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
   <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h4 class="modal-title" id="myModalLabel">添加课程</h4>
        </div>
        <div class="modal-body">
		   <h5>课程名称</h5>
		   <input class="form-control" id="course_name" type="text" placeholder="" style="width:200px;">
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" id="addCourse" class="btn btn-primary"> 添加</button>
        </div>
      </div><!-- /.modal-content -->
   </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal fade" id="updatecourseModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">修改课程</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" id="update_data_id" value=""/>
                <h5>课程名称</h5>
                <input class="form-control" type="text" id="update_course_name" placeholder="" style="width:200px;">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="saveCourse" class="btn btn-primary"> 保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<div class="modal fade" id="deletecourseModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="mySmallModalLabel">删除课程</h4>
            </div>
            <input type="hidden" id="delete_data_id" value=""/>
            <div class="modal-body">
                <button type="button" style="margin-left:35px" class="btn btn-default delete-modal-cancel" data-dismiss="modal">&nbsp;取&nbsp;&nbsp;消&nbsp;&nbsp;</button>
                <button type="button" style="margin-left:30px" class="btn btn-danger delete-modal-ensure">&nbsp;&nbsp;删&nbsp;&nbsp;除&nbsp;&nbsp;</button>
            </div>
        </div><!-- /.modal-content -->
    </div>
</div><!-- /.modal -->
<script src="/res/js/jquery.min.js"></script>
<script src="/bootstrap/js/bootstrap.min.js"></script>

<script src="/res/js/toast.js"></script>

<script>
$(function () { 
	$('#myModal').modal({
		keyboard: true
	})
	
	$('#addCourse').click(function(){
        var course_name = $('#course_name').val();

        if(course_name =='') {
            new Toast({context:$('body'),top:200 ,time:2000, message:'课程名称不能为空'}).error();
            return false;
        }

        var url = '/course/add';
        var data = {"course_name":course_name};
        $.post(url, data, function(ret){
            if(ret.en == 200) {
                new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).success();
                setTimeout(function(){
                    window.location.href = '/course/index';
                } , 2000);
            } else {
                new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).error();
            }
        });
    });

    /** "修改" 触发模态框显示 */
    $('.update_course').click(function(){
        var _id = $(this).attr('_id');
        $('#update_data_id').val(_id);

        var course_name = $(this).attr('course_name');
        $('#update_course_name').val(course_name);
    });

    $('#saveCourse').click(function(){
        var update_data_id = $('#update_data_id').val();
        var update_course_name = $('#update_course_name').val();

        if(update_data_id == '') {
            new Toast({context:$('body'),top:200 ,time:2000, message:'课程不能为空'}).error();
            return false;
        }
        if(update_course_name =='') {
            new Toast({context:$('body'),top:200 ,time:2000, message:'课程名称不能为空'}).error();
            return false;
        }

        var url = '/course/save';
        var data = {"_id":update_data_id, "course_name":update_course_name};
        $.post(url, data, function(ret){
            if(ret.en == 200) {
                new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).success();
                setTimeout(function(){
                    window.location.href = '/course/index';
                } , 2000);
            } else {
                new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).error();
            }
        });
    });

    /** "删除" 触发模态框显示 */
    $('.delete_course').click(function(){
        var _id = $(this).attr('_id');
        $('#delete_data_id').val(_id);
    });

    /** 执行 "删除" */
    $('.delete-modal-ensure').click(function(){
        var _id = $('#delete_data_id').val();
        var request_api = '/course/delete';
        if(_id != undefined && _id != '') {
            $.post(request_api ,{"_id":_id} ,function(ret) {
                if(ret.en == 200) {
                    new Toast({context:$('body'),top:200 ,time:1000 ,message:ret.em}).success();
                    setTimeout(function(){
                        $('#deleteclassModal').modal('hide');
                        window.location.reload();
                    } , 1000);
                } else {
                    new Toast({context:$('body'),top:200 ,time:1000, message:ret.em}).error();
                }
            });
        } else {
            new Toast({context:$('body'),top:200 ,time:1000, message:"参数不全"}).error();
        }
    });
});
</script>

</body>
</html>